// 用户相关
var router = require('../common');
var userModule = require('../../model/userMode')
// 获取所有用户列表
router.get('/userList', (req, res) => {
    let { currentpage, limit = 10 } = req.query;
    limit = limit - 0;
    userModule.countDocuments().then(async (count) => {
        try {
            let data = await userModule.find({}, { __v: 0 }).skip(((currentpage - 0) - 1) * limit).limit(limit).sort({ _id: -1 })
            res.json({
                code: 0,
                count,
                list: data
            })
        } catch (err) {
            res.status(500).json({
                code: 1,
                msg: err
            })
        }
    })  
})
// 修改用户权限
router.post('/user/admin', (req, res) => {
    let { jurisdiction, id } = req.body;
    jurisdiction = jurisdiction == 1 ? true : false;
    async function getData() {
        let data = await userModule.findOne({ _id: id });
        let doc = await userModule.updateOne({ _id: id }, { $set: { isAdmin: jurisdiction } });
        if (data) {
            res.json({
                code: 0,
                msg: doc
            })
        } else {
            res.json({
                code: 1,
                msg: '未找到该用户'
            })
        }
    }
    getData();
})
// 修改用户和密码
router.post('/update', async (req, res) => {
    try {
        let { id, username, pwd } = req.body;
        if (username == undefined || pwd == undefined || id == undefined) {
            return res.json({
                code: 2,
                msg: '缺少参数'
            })
        };
        let allUser = await userModule.find({ _id: { $ne: id } });
        // console.log(allUser.some(item=>item.username == username));
        if (allUser.some(item => item.username == username)) {
            return res.json({
                code: 3,
                msg: '换个用户名吧，已经有人使用了'
            })
        } else {
            let data = await userModule.updateOne({ _id: id }, { $set: { password: pwd ,username} });
            res.json({
                code: 0,
                data
            })
        }

    } catch (err) {
        res.status(500).json({
            code: 9,
            msg: err
        })
    }

})
// 删除
router.post('/delete', async (req, res) => {
    try {
        console.log(req.body);
        let { id } = req.body;
        let isId = await userModule.findOne({ _id: id });
        if (isId) {
            let data = await userModule.deleteOne({ _id: id });
            res.json({
                code: 0,
                msg: data
            })
        } else {
            res.json({
                code: 1,
                msg: '请刷新页面在操作'
            })
        }

    } catch (err) {
        res.status(500).json({
            code: 9,
            msg: err
        })
    }

})

// 添加用户收藏列表
router.post('/add/collect',(req,res)=>{
    try{
        res.json({code:0,msg:'LJKSJLSKJFLJSLD'})
    }catch(err){res.status.json({code:9,msg:err})}
})
module.exports = router